package com.huadao.cart_ccs.experiment.log.exception;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseListFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.experiment.log.OnDay;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.adapter.FooterHolder;
import com.huadao.cart_ccs.common.adapter.HeaderHolder;
import com.huadao.cart_ccs.common.adapter.onItemCommonClickListener;
import com.huadao.cart_ccs.dialog.DialogUtil;
import com.huadao.cart_ccs.exception.ExceptionType;
import com.huadao.cart_ccs.greendao.entity.ExceptionInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 程序异常
 */
public class ExceptionFrag extends BaseListFragment implements OnDay,ExceptionContract.IExceptionView {

    private ExceptionPresenter exceptionPresenter;
    private List<ExceptionInfo> exceptionInfoList;

    public static ExceptionFrag getInstance(int day){
        Bundle bundle=new Bundle();
        bundle.putInt("day",day);
        ExceptionFrag exceptionFrag=new ExceptionFrag();
        exceptionFrag.setArguments(bundle);
        return exceptionFrag;
    }

    public void setDayBundle(int day){
        Bundle bundle=new Bundle();
        bundle.putInt("day",day);
        this.setArguments(bundle);
    }

    @Override
    protected BasePresenter getPresenter() {
        return exceptionPresenter;
    }

    @Override
    public int getContentViewId() {
        return R.layout.frag_list;
    }

    @Override
    public int getRecycleViewId() {
        return R.id.recycleView;
    }

    @Override
    public int getSwipeRefreshLayoutId() {
        return R.id.swipeRefreshLayout;
    }

    @Override
    protected void loadData() {
        exceptionPresenter.getExceptionsList();
    }

    @Override
    protected void loadMoreData() {

    }

    @Override
    protected void init() {
        exceptionPresenter = new ExceptionPresenter(this);
        Bundle bundle=getArguments();
        if(bundle!=null&&bundle.containsKey("day")){
            int day=bundle.getInt("day");
            exceptionPresenter.setDay(day);
        }
        setEnableLoadMore(false);
        exceptionInfoList=new ArrayList<>();
    }

    @Override
    protected RecyclerView.ItemDecoration setItemDecoration() {
        return new DividerItemDecoration(this.getContext(), DividerItemDecoration.VERTICAL);
    }

    @Override
    protected CommonRecycleAdapter setAdapter() {
        return new ExceptionAdapter(mContext,exceptionInfoList,R.layout.rv_exception_item);
    }

    @Override
    public RecyclerView.LayoutManager setLayoutManage() {
        return new LinearLayoutManager(this.getContext());
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        exceptionPresenter.getExceptionsList();
    }

    @Override
    public void showExceptionList(List exceptionFileList) {
        this.exceptionInfoList.clear();
        this.exceptionInfoList.addAll(exceptionFileList);
        notifyDataSetChanged();
        setSwipeRefresh(false);
    }

    @Override
    public void daySelected(int day) {
        if(day!=exceptionPresenter.getDay()) {
            exceptionPresenter.setDay(day);
            onRefresh();
        }
    }

    class ExceptionAdapter extends CommonRecycleAdapter<ExceptionInfo> {

        private DialogUtil dialogUtil;
        private onItemCommonClickListener commonClickListener=new onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                ExceptionInfo exceptionInfo=exceptionInfoList.get(position);
                if(exceptionInfo.getType()== ExceptionType.ERROR_SYSTEM) {
                    String content = exceptionPresenter.getExceptionsFile(exceptionInfo.getMessage());
                    dialogUtil.showMessageDialog(content, null);
                }else if(exceptionInfo.getType()== ExceptionType.ERROR_SERIALPORT){
                    dialogUtil.showMessageDialog(exceptionInfo.getMessage(), null);
                }else if(exceptionInfo.getType()== ExceptionType.ERROR_LOGIC){
                    dialogUtil.showMessageDialog(exceptionInfo.getMessage(), null);
                }
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        };

        public ExceptionAdapter(Context context, List<ExceptionInfo> dataList, int layoutId) {
            super(context, dataList, layoutId);
            dialogUtil=new DialogUtil(context);
        }

        @Override
        protected void bindData(CommonViewHolder holder, ExceptionInfo data) {
            holder.setText(R.id.tv_exception,data.getMessage()).setCommonClickListener(commonClickListener);
            if(data.getDate()!=null) {
                holder.setText(R.id.tv_date, TimeUtil.log_sdf.format(data.getDate()));
            }
            holder.setText(R.id.tv_code,data.getCodeString());
        }

        @Override
        protected void bindFooterData(FooterHolder holder) {

        }

        @Override
        protected void bindHeaderData(HeaderHolder holder) {

        }
    }
}
